{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pathlib,os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-5-7051e5496627>, line 5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-5-7051e5496627>\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m    path.suffix()　　　#文件后缀\u001b[0m\n\u001b[1;37m                 　　^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "#path = Path(__file__)\n",
    "\n",
    "path.suffix　　　#文件后缀\n",
    "path.stem　　　　　 #文件名不带后缀\n",
    "path.name　　　　　　#带后缀的完整文件名\n",
    "path.parent　　　　#路径的上级目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('C:\\\\',\n",
       " 'Users',\n",
       " 'user',\n",
       " 'OneDrive',\n",
       " 'code space',\n",
       " 'python',\n",
       " '1notebook',\n",
       " 'os and file')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p=pathlib.Path(pathlib.Path.cwd())\n",
    "\n",
    "p.parts\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{\"result\": 0, \"message\": \"PATH ERROR /04data/python/share_path/searchImages/7e20daa8-1024-4db4-8b21-8b84aecea37520181221140402\", \"data\": {\"id_info\": []}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WindowsPath('C:/Users/user/OneDrive/code space/python/1notebook/0123')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.with_name(\"0123\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WindowsPath('C:/Users/user/OneDrive/code space/python/1notebook/os and file/2312/sdsd')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t=(\"2312\",\"sdsd\")\n",
    "p.joinpath(*t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "基本用法:\n",
    "\n",
    "Path.iterdir()　　#遍历目录的子目录或者文件\n",
    "\n",
    "Path.is_dir()　　#判断是否是目录\n",
    "\n",
    "Path.glob()　　#过滤目录(返回生成器)\n",
    "\n",
    "Path.resolve()　　#返回绝对路径\n",
    "\n",
    "/　　# 拼接路径(目录中进行导航-官网说法)\n",
    "\n",
    "Path.exists()　　#判断路径是否存在\n",
    "\n",
    "Path.open()　　#打开文件(支持with)\n",
    "\n",
    "Path.unlink()　　#删除文件或目录(目录非空触发异常)\n",
    "\n",
    "基本属性:\n",
    "\n",
    "Path.parts　　#分割路径 类似os.path.split(), 不过返回元组\n",
    "\n",
    "Path.drive　　#返回驱动器名称\n",
    "\n",
    "Path.root　　#返回路径的根目录\n",
    "\n",
    "Path.anchor　　#自动判断返回drive或root\n",
    "\n",
    "Path.parents　　#返回所有上级目录的列表\n",
    "\n",
    " \n",
    "\n",
    "改变路径:\n",
    "\n",
    "Path.with_name()　　#更改路径名称, 更改最后一级路径名\n",
    "\n",
    "Path.with_suffix()　　#更改路径后缀\n",
    "\n",
    "/　　#拼接路径\n",
    "\n",
    "Path.joinpath()　　#拼接路径\n",
    "\n",
    "Path.relative_to()　　#计算相对路径\n",
    "\n",
    " \n",
    "\n",
    "测试路径:\n",
    "\n",
    "Path.match()　　#测试路径是否符合pattern\n",
    "\n",
    "Path.is_dir()　　#是否是文件\n",
    "\n",
    "Path.is_absolute()　　#是否是绝对路径\n",
    "\n",
    "Path.is_reserved()　　#是否是预留路径\n",
    "\n",
    "Path.exists()　　#判断路径是否真实存在\n",
    "\n",
    " \n",
    "\n",
    "其他方法:\n",
    "\n",
    "Path.cwd()　　#返回当前目录的路径对象\n",
    "\n",
    "Path.home()　　#返回当前用户的home路径对象\n",
    "\n",
    "Path.stat()　　#返回路径信息, 同os.stat()\n",
    "\n",
    "Path.chmod()　　#更改路径权限, 类似os.chmod()\n",
    "\n",
    "Path.expanduser()　　#展开~返回完整路径对象\n",
    "\n",
    "Path.mkdir()　　#创建目录\n",
    "\n",
    "Path.rename()　　#重命名路径\n",
    "\n",
    "Path.rglob()　　#递归遍历所有子目录的文件"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
